<?php 
class cogClassAction extends ActionManage
{
	public function defaultAction()
	{
		$type = $this->get('type');
		
		
		$cjinfo = array(
			'mobile'=>'',
			'name' => ''
		);
		$uid  = $this->companyinfo['uid'];
		$ucrs = m('usera')->getone('`cid`='.$this->companyid.' and `uid`='.$uid.'');
		if($ucrs){
			$cjinfo['name'] 	= $ucrs['name'];
			$cjinfo['mobile']  = $ucrs['mobile'];
		}
		
		$this->assign('type', $type);
		$this->assign('cjinfo', $cjinfo);
	}
	
	public function savecheckAction()
	{
		$type = $this->post('type');
		
		if(!$type){
			$uarr['name'] = $this->post('name');
			$uarr['shortname'] = $this->post('shortname');
			$uarr['contacts'] = $this->post('contacts');
			$uarr['logo'] = $this->post('logo');
			$uarr['tel'] = $this->post('tel');
			if(isempt($uarr['name']))return '单位名称不能为空';
			m('company')->update($uarr, $this->companyid);
		}
		
		if($type=='create'){
			$createid = (int)$this->post('createid','0');
			if($createid>0){
				$ars = m('usera')->getone('`cid`='.$this->companyid.' and `id`='.$createid.'');
				if(!$ars)return '此用户不存在';
				$status = $ars['status'];
				if($status!=1)return '此用户未关注';
				$uid 	= $ars['uid'];
				$urs 	= m('users')->getone('`id`='.$uid.' and `status`=1');
				if(!$urs)return '此平台用户不存在';
				$olduid = $this->companyinfo['uid'];
				m('company')->update('`uid`='.$uid.'', $this->companyid);
				m('usera')->update('`type`=0', '`cid`='.$this->companyid.' and `uid`='.$olduid.'');
				m('usera')->update('`type`=2', '`cid`='.$this->companyid.' and `uid`='.$uid.'');
			}
		}
		
		if($type=='hdiao'){
			$uarr['hdiaourl'] = $this->post('hdiaourl');
			$uarr['hdiaokey'] = $this->post('hdiaokey');
			m('company')->update($uarr, $this->companyid);
		}
		
		if($type=='jiesan'){
			if($this->adminid!=$this->companyinfo['uid'])return '非创建人无法操作';
			$device 	= $this->post('device');
			$code 		= trim($this->post('smscode'));
			$mobile 	= trim($this->post('mobile'));
			if(isempt($code))return '短信验证码不能为空';
			if(!c('sms')->checkcode($mobile, $code, 'san', $device))return '手机短信验证码错误';
			$tables 	= explode(',','agent,chatgroup,chatgroupuser,dept,history,mess,messzt,party,usera,wenti,option');
			$where 		= '`cid`='.$this->companyid.'';
			foreach($tables as $tab)m($tab)->delete($where);
			$frows 		= m('file')->getall($where);
			foreach($frows as $k=>$rs){
				$filepath = $rs['filepath'];
				if(!isempt($filepath) && file_exists($filepath))@unlink($filepath);
				$filepath = $rs['thumbpath'];
				if(!isempt($filepath) && file_exists($filepath))@unlink($filepath);
			}
			m('file')->delete($where);
			m('company')->delete($this->companyid);
		}
		
		return 'ok';
	}
	
	public function updatesecretAction()
	{
		$secret = md5($this->jm->getRandkey());
		$uarr['secret'] = $secret;
		m('company')->update($uarr, $this->companyid);
		return 'ok';
	}
}